package cn.ac.jaas.ljx.wordsegmentation.interfaces.impl;

import java.util.ArrayList;
import java.util.List;

import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import cn.ac.jaas.ljx.wordsegmentation.domain.Document;
import cn.ac.jaas.ljx.wordsegmentation.domain.Word;
import cn.ac.jaas.ljx.wordsegmentation.interfaces.Segmenter;
import cn.ac.jaas.ljx.wordsegmentation.util.WordCountUtil;
import org.springframework.stereotype.Service;

@Service("ansjSegmenter")
public class AnsjSegmenter implements Segmenter {

	@Override
	public List<Word> segment(Document	document) {
		String text = document.getTitle() + document.getDigest();
		Result result = ToAnalysis.parse(text);
		List<Term> terms = result.getTerms();
		List<Word> wordsNoCount = new ArrayList<>();
		for(Term term : terms) {
			Word word = new Word();
			word.setValue(term.getRealName());
			word.setNature(term.getNatureStr());
			wordsNoCount.add(word);
		}
		List<Word> wordsCounted = new ArrayList<>();
		WordCountUtil.wordCount(wordsNoCount, wordsCounted);
		return wordsCounted;
	}

}
